/**
 * 
 * @brief  bsp_spi.h
 * @param  None
 * 
 * @author xhq
 * @date   2024/11/05
 * 
 */
#ifndef __SRC_BSP_SPI_H__
#define __SRC_BSP_SPI_H__

/* include */
#include <linux/spi/spidev.h>
#include <sys/ioctl.h>

#include "typedef.h"
#include "global_variables.h"

/* define */
#define     SPI_RECIEVE_BUF_MAX_LEN         16384
#define     SPI_NUM_MAX                     2
/* typedef */
typedef struct {
    uint32_t spi_mode;
    uint32_t spi_speed;
    uint8_t spi_bits;
    uint16_t spi_delay;
    struct{
        int fd;
        char* dev_name;
    }dev_info[SPI_NUM_MAX];
}spi_dev_type;

/* global varibles */
extern spi_dev_type g_spi_dev_info;

/* private function */

/* function */
uint32_t bsp_spi_init(void);
uint32_t bsp_spi_read(void);
uint32_t bsp_spi_write(void);
uint32_t spi_read(int fd,uint8_t *rx_buf, uint32_t rx_len);
uint32_t spi_write(int fd,uint8_t *cmd_buf, uint8_t cmd_len, uint8_t *tx_buf, uint32_t tx_len);
uint32_t spi_write_crc(int fd,uint8_t *cmd_buf, uint8_t cmd_len, uint8_t *tx_buf, uint32_t tx_len, uint8_t *crc_buf);
uint32_t spi_transfer(int fd, uint8_t const *tx, uint8_t const *rx, size_t len);
uint32_t spi_read_transfer_crc(int fd,uint8_t *cmd_buf, uint8_t cmd_len, uint8_t *rx_buf, uint32_t rx_len, uint8_t *crc_buf);
uint32_t spi_read_transfer(int fd,uint8_t *cmd_buf, uint8_t cmd_len, uint8_t *rx_buf, uint32_t rx_len);
uint32_t spi_write_transfer_crc(int fd,uint8_t *cmd_buf, uint8_t cmd_len, uint8_t *tx_buf, uint32_t tx_len, uint8_t *crc_buf);
uint32_t spi_write_transfer(int fd,uint8_t *cmd_buf, uint8_t cmd_len, uint8_t *tx_buf, uint32_t tx_len);
uint32_t spi_close(int fd);



#endif